package com.cskaoyan.user.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.UserLoginOutResponse;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dto.KaptchaCodeRequest;
import com.cskaoyan.user.dto.KaptchaCodeResponse;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.dto.bo.UserLoginBo;
import com.cskaoyan.user.dto.vo.UserLoginVo;
import com.cskaoyan.user.service.UserLoginService;
import com.cskaoyan.user.service.impl.KaptchaServiceImpl;

/**
 * @author zyf
 * @since 2022/06/17 16:17
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    UserLoginService userLoginService;

    @Autowired
    KaptchaServiceImpl kaptchaService;

    @Autowired
    UserConverterMapper userConverterMapper;



    @PostMapping("/login")
    public ResponseData loginPost(@RequestBody UserLoginBo userLoginBo, HttpServletRequest request, HttpServletResponse response2) {

        String captcha = userLoginBo.getCaptcha();
        String userName = userLoginBo.getUserName();
        String userPwd = userLoginBo.getUserPwd();

        //验证码校验
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);

        String code = response.getCode();
        if (!code.equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }

        UserLoginVo userLoginVo = userLoginService.loginPost(userLoginBo, request);

        if (userLoginVo.getUsername() != userName) {
            return new ResponseUtil<>().setErrorMsg("用户名或密码错误");
        }


        Member member=userLoginService.selectMember(userLoginBo);

        UserLoginResponse loginResponse = userConverterMapper.converter(member);

        Long uid=userLoginService.selectIdByUserName(userName);

        userLoginVo.setUid(uid);

        // String data=userName;
        // JwtTokenUtils build = JwtTokenUtils.builder().msg(data).build();
        JwtTokenUtils build = JwtTokenUtils.builder().msg(String.valueOf(JSON.toJSONString(userLoginVo))).build();
        String tokenStr = build.creatJwtToken();



        Cookie cookie= CookieUtil.genCookie("access_token",tokenStr,"/",86400);
        //cookie.setHttpOnly(true);
        response2.addCookie(cookie);

        loginResponse.setToken(tokenStr);


        return new ResponseUtil<>().setData(loginResponse);
    }

    @GetMapping("/login")
    public ResponseData loginGet(HttpServletRequest request){

        // String token = CookieUtil.getCookieValue(request, "access_token");
        //
        // String userName = JwtTokenUtils.builder().token(token).build().freeJwt();
        //
        // Long uid=userLoginService.selectIdByUserName(userName);

        String userInfo = (String) request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        String userName = object.get("username").toString();

        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setUsername(userName);
        userLoginVo.setUid(uid);

        //UserLoginVo userLoginVo=userLoginService.loginGet(request);

        return new ResponseUtil<>().setData(userLoginVo);
    }


    /**
     * @author wy
     * @since 2022/6/18 22:51
     * 实现功能...退出登录
     */
    @GetMapping("/loginOut")
    public ResponseData loginOut(HttpServletRequest request,HttpServletResponse response2){
        UserLoginOutResponse response = userLoginService.loginOut(request,response2);

        if(UserRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil().setData(null);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
